<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

use function Matrix\add;

//引入hash

class GiftBoxPrizeRepository
{

    /**
     * 新增
     *
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function addBlindBox($param, $action)
    {
        if ($action == 1) {
            $model = app("model_gift_box_prize");
        } else {
            $model = app("model_gift_box_prize_next");
        }

        $addConfig = $model->create($param);

        if ($addConfig) {
            return $addConfig;
        }
        throw new BasicException(93045);
    }

    /**
     * 删除
     */
    public function delBlindBox($idArr = [], $action)
    {
        if ($action == 1) {
            $model = app("model_gift_box_prize");
        } else {
            $model = app("model_gift_box_prize_next");
        }

        $res = $model->whereIn("id", $idArr)->delete();

        if ($res) {
            return $res;
        }
        throw new BasicException(93044);
    }

    /**
     * 修改
     */
    public function saveBlindBox($where, $param, $action)
    {
        if ($action == 1) {
            $model = app("model_gift_box_prize");
        } else {
            $model = app("model_gift_box_prize_next");
        }
        if (isset($param['action'])) {
            unset($param['action']);
        }

        $saveConfig = $model->where($where)->update($param);

        //        if ($saveConfig) return $saveConfig;
        //        throw new BasicException(93047);
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getBlindBoxQueryList(array $params, $action)
    {
        if ($action == 1) {
            $model = app("model_gift_box_prize");
        } else {
            $model = app("model_gift_box_prize_next");
        }

        $whereArr = [];

        if (!empty($params["type"])) {
            $whereArr[] = ["t1.type", "=", $params["type"]];
        }

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $giftModel = app('model_gift');

        $result = $model
            ->from($model->getTable() . " as t1")
            ->select([
                "t1.*",
                "t2.name",
                "t2.price",
                "t2.picture",
                "t2.special",
            ])
            ->leftJoin(DB::raw($giftModel->getTable() . " as t2"), "t1.gift_id", "=", "t2.id")
            ->where($whereArr)->where($whereFunc)->orderBy('t1.id', 'asc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }


    /** 获取盲盒配置说明
     *
     * @param  int  $type  类型 1-银盲盒； 2-铜盲盒；3-金盲盒；
     * @param  int  $perPage
     * @param  int  $page
     * @return mixed
     */
    public function getBlindBoxConfigList(int $type = 0, int $perPage, int $page)
    {
        $model = app('model_blind_box_config_gift');
        $giftModel = app('model_gift');
        $result = $model
            ->from($model->getTable() . " as t1")
            ->select([
                "t1.*",
                "t2.name",
                "t2.price",
                "t2.picture",
                "t2.special",
            ])
            ->leftJoin(DB::raw($giftModel->getTable() . " as t2"), "t1.gift_id", "=", "t2.id");
        $whereArr = [
            't1.box_type' => $type,
        ];
        if ($type) {
            $result = $result->where($whereArr)
                ->orderBy('t1.probability', 'asc')
                ->paginate($perPage, ['*'], '', $page);
        } else {
            $result = $result
                ->orderBy('t1.probability', 'asc')
                ->paginate($perPage, ['*'], '', $page);
        }
        
        return $result;
    }
}